package com.example.myapplication.test;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.R;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.mvp.home.HomeContract;
import com.example.myapplication.setting.SettingFragment;
import com.example.myapplication.test.SimpleAvtivity;
import com.example.myapplication.utils.DialogHelper;
import com.example.myapplication.utils.ScreenUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static android.app.Activity.RESULT_OK;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.appcompat.widget.Toolbar;

import android.Manifest;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.text.TextUtils;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.myapplication.Html5Activity;
import com.example.myapplication.R;
import com.example.myapplication.app.AppUtils;
import com.example.myapplication.app.Cache;
import com.example.myapplication.app.Url;
import com.example.myapplication.app.app;
import com.example.myapplication.data.beanv2.CourseGroup;
import com.example.myapplication.data.beanv2.UserWrapper;

import com.example.myapplication.mvp.add.AddActivity;
import com.example.myapplication.mvp.login.SignActivity;



import com.example.myapplication.utils.DialogHelper;
import com.example.myapplication.utils.DialogListener;
import com.example.myapplication.utils.RequestPermission;
import com.example.myapplication.utils.ScreenUtils;
import com.example.myapplication.utils.event.SignEvent;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.common.Constant;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Objects;

import de.hdodenhof.circleimageview.CircleImageView;

import com.example.myapplication.setting.SettingActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class getNameout extends AppCompatActivity {

    private static final int MAX_TOKENS = 10;
    private static final long REFILL_INTERVAL = 1000; // 每秒生成一个令牌

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_impt);

        String urlString = "http://pay.ucas.ac.cn/pay/allPay.html";
        String cookie = ""; // Replace this with your actual cookie value

        TokenBucket tokenBucket = new TokenBucket(MAX_TOKENS, REFILL_INTERVAL);
        new FetchPageTask(tokenBucket).execute(urlString, cookie);
    }

    private static class FetchPageTask extends AsyncTask<String, Void, String> {
        private final TokenBucket tokenBucket;

        public FetchPageTask(TokenBucket tokenBucket) {
            this.tokenBucket = tokenBucket;
        }

        @Override
        protected String doInBackground(String... params) {
            if (!tokenBucket.tryConsume()) {
                Log.w("FetchPageTask", "Rate limit exceeded, try again later");
                return null;
            }

            String urlString = params[0];
            String cookie = params[1];

            try {
                URL url = new URL(urlString);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                // Set request method
                connection.setRequestMethod("GET");

                // Set request headers
                connection.setRequestProperty("Cookie", cookie);

                // Get response code
                int responseCode = connection.getResponseCode();
                Log.d("Response Code", String.valueOf(responseCode));

                // Read response content
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                return response.toString();

            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String response) {
            if (response != null) {
                // Parse HTML using Jsoup
                Document doc = Jsoup.parse(response);

                // Extract payment amount
                Elements amountElements = doc.select("#lastPay_amount");
                for (Element element : amountElements) {
                    Log.d("Payment Amount", element.text());
                }

                // Extract name
                Elements nameElements = doc.select("/html/body/div[1]/div/div[2]/div[2]/div[1]/div[1]/span");
                for (Element element : nameElements) {
                    Log.d("Name", element.text());
                }
            }
        }
    }

    public static class TokenBucket {
        private final int maxTokens;
        private final long refillInterval;
        private int tokens;
        private long lastRefillTime;

        public TokenBucket(int maxTokens, long refillInterval) {
            this.maxTokens = maxTokens;
            this.refillInterval = refillInterval;
            this.tokens = maxTokens;
            this.lastRefillTime = System.currentTimeMillis();
        }

        public synchronized boolean tryConsume() {
            refill();

            if (tokens > 0) {
                tokens--;
                return true;
            }

            return false;
        }

        private void refill() {
            long now = System.currentTimeMillis();
            long elapsed = now - lastRefillTime;
            if (elapsed > refillInterval) {
                int newTokens = (int) (elapsed / refillInterval);
                tokens = Math.min(tokens + newTokens, maxTokens);
                lastRefillTime = now;
            }
        }
    }
}
